package ide;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JToolBar;

import datastruct.DBStructure;
import datastruct.Tables;
import dbConnections.MsAccessConnection;
import filefilter.MDBFileFilter;
import filefilter.XMLFileFilter;
import xml.XMLWriter;

public class ToolBarMain extends JToolBar implements MouseListener{
	private static final long serialVersionUID = 1L;
	
	public ToolBarMain()
	{
		super(JToolBar.HORIZONTAL);
		this.setVisible(true);
		createButtons();
	}
	
	private void createButtons()
	{
		JButton button;
		
		button = new JButton();
		button.setText("NUOVO");
		button.setAlignmentX(JButton.CENTER_ALIGNMENT);
		button.setAlignmentY(JButton.CENTER_ALIGNMENT);
		button.addMouseListener(this);
		this.add(button);
		
		button = new JButton();
		button.setText("APRI");
		button.setAlignmentX(JButton.CENTER_ALIGNMENT);
		button.setAlignmentY(JButton.CENTER_ALIGNMENT);
		button.addMouseListener(this);
		this.add(button);
		
		button = new JButton();
		button.setText("IMPORTA");
		button.setAlignmentX(JButton.CENTER_ALIGNMENT);
		button.setAlignmentY(JButton.CENTER_ALIGNMENT);
		button.addMouseListener(this);
		this.add(button);
		
		button = new JButton();
		button.setText("SALVA");
		button.setAlignmentX(JButton.CENTER_ALIGNMENT);
		button.setAlignmentY(JButton.CENTER_ALIGNMENT);
		button.addMouseListener(this);
		this.add(button);
		
		button = new JButton();
		button.setText("ESCI");
		button.setAlignmentX(JButton.CENTER_ALIGNMENT);
		button.setAlignmentY(JButton.CENTER_ALIGNMENT);
		button.addMouseListener(this);
		this.add(button);
	}
	
	private void importXML()
	{
		JFileChooser fc = new JFileChooser();
		fc.addChoosableFileFilter(new XMLFileFilter());
		fc.setAcceptAllFileFilterUsed(false);
		fc.showDialog(this, "Apri");
		File f = fc.getSelectedFile();
		ScrollPaneTreeView.getTree().read(f);
	}
	
	private void openDB()
	{
		JFileChooser fc = new JFileChooser();
		fc.addChoosableFileFilter(new MDBFileFilter());
		fc.setAcceptAllFileFilterUsed(false);
		fc.showDialog(this, "Apri DB");
		File f = fc.getSelectedFile();
		
		MsAccessConnection.getInstance().getConnection(f.getAbsolutePath());
		DBStructure dbStruct = MsAccessConnection.getInstance().dbStructure();
		XMLWriter xml = new XMLWriter(dbStruct);
		try {
			xml.create();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		fc.showSaveDialog(this);
		f = fc.getSelectedFile();
		try {
			xml.write(f);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		JButton button = (JButton) e.getSource();
		if(button.getText().equalsIgnoreCase("ESCI"))
			System.exit(0);
		if(button.getText().equalsIgnoreCase("IMPORTA"))
			importXML();
		if(button.getText().equalsIgnoreCase("NUOVO"))
			openDB();
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
